package com.info.zhiduoduo.weixin.controller;

import com.google.common.io.ByteStreams;
import com.info.zhiduoduo.common.annotation.ZddLog;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.XmlUtil;
import com.info.zhiduoduo.weixin.formbean.weixin.WxCodeFormBean;
import com.info.zhiduoduo.weixin.service.WxService;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@RestController
@RequestMapping("/wx")
public class WeixinController extends CommonController {

	@Autowired
	WxService wxService;

	@RequestMapping("/notify")
	public String wxNotify(HttpServletRequest request) {
		//LogUtil.info(JSON.toJSONString(o));

		/*String echostr = request.getParameter("echostr");
		LogUtil.info(request.getParameterMap().toString());
		return echostr;*/

			  /*LogUtil.info(signature);
		LogUtil.info(timestamp + "");
		LogUtil.info(nonce);
		LogUtil.info(echostr);
		try {
			if (WXPublicUtils.verifyUrl(signature, timestamp, nonce)) {
				response.setContentType("text/plain");
				PrintWriter writer = response.getWriter();
				writer.write(echostr);
				writer.flush();
				writer.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/

		try {
			String xml = new String(ByteStreams.toByteArray(request.getInputStream()));
			log.info("请求体xml=" + xml);
			String json = XmlUtil.xml2jsonString(xml);
			log.info("请求体转为json=" + json);
			JSONObject jo = new JSONObject(json);

		} catch (IOException e) {
			e.printStackTrace();
		}

		//回复空字符串表示已经收到事件通知，微信服务器就不会重复发送相同的事件。
		return "";
	}

	/**
	 公众号：前端网页授权后将code传给后台，通过code获取用户信息，注册用户；静默授权

	 @param formBean
	 @return
	 */
	@ZddLog(name = "公众号：前端网页授权后将code传给后台，通过code获取用户信息，注册用户；静默授权")
	@PostMapping("/silentAuth")
	public SmartResult wxWebAuthorize(@RequestBody BaseSimpleFormBean<WxCodeFormBean> formBean) {
		return wxService.wxWebAuthorize(formBean);
	}

	/**
	 公众号：网页授权的另外一种方式。 用户点击按钮，直接重定向到后台 请求微信授权。 即 微信直接将code传给后台。

	 @param request
	 @param response
	 */
	@RequestMapping("/authPage")
	public void wxClickHomePage(HttpServletRequest request, HttpServletResponse response) {
		wxService.wxClickHomePage(request, response);
	}

	/**
	 公众号：分享功能，获取token及签名

	 @param formBean
	 @return
	 */
	@RequestMapping("/share")
	@ResponseBody
	public SmartResult wxShare(@RequestBody BaseSimpleFormBean<String> formBean) {
		return wxService.wxShare(formBean);
	}

	@RequestMapping("/sendMessage")
	@ResponseBody
	public SmartResult sendMessage(@RequestBody BaseSimpleFormBean formBean) {
		return wxService.sendMessage(formBean);
	}

	@PostMapping("/refreshToken")
	@ResponseBody
	public SmartResult refreshAccessToken(@RequestBody BaseSimpleFormBean formBean) {
		return wxService.refreshAccessToken();
	}

	@PostMapping("/userState")
	public SmartResult getWxUserState(@RequestBody BaseSimpleFormBean<String> formBean) {
		return wxService.getWxUserState(formBean);
	}
}
